#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
const int N = 1e6 + 5;

map<string, string> fa;

string find(string x) {
    if (x == fa[x]) return x;
    return fa[x] = find(fa[x]);
}

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        fa[s] = s;
    }
    while (m--) {
        int op;
        cin >> op;
        string a, b;
        cin >> a >> b;
        if (op == 1) {
            a = find(a), b = find(b);
            if (a != b) fa[a] = b;
        } else {
            a = find(a), b = find(b);
            if (a != b)
                cout << 0 << endl;
            else
                cout << 1 << endl;
        }
    }
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    solve();

    return 0;
}